package com.gaofei.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.gaofei.dao.UserMapper;
import com.gaofei.domain.Cart;
import com.gaofei.domain.CartVo;
import com.gaofei.domain.HgUser;
import com.gaofei.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	UserMapper userMapper;
	@Override
	public HgUser findUserByTelephone(HgUser portalUser) {
		return userMapper.findUserByTelephone(portalUser);
	}
	@Override
	public HgUser findUserByUsernameAndPassword(HgUser portalUser) {
		return userMapper.findUserByUsernameAndPassword(portalUser);
	}
	
	//加入购物车
	@Override
	public void addCart(CartVo vo) {
		//添加购物车的时候,记得要判断什么时候合并购物车
		Cart cart = userMapper.findCartByUidAndSkuid(vo);//根据用户id和skuid查询数据库是否存在该购物车
		System.err.println(cart);
		if(cart!=null) {
//			说明数据库中已经存在
			//当用户id和skuid 与 第二次添加的购物车信息一样的情况下,我们就合并
			//合并购物车
			BigDecimal preSumTotal = cart.getSumTotal();//原来
			BigDecimal newSumTotal = vo.getSumTotal();//新的价格
			System.out.println(newSumTotal+"====="+preSumTotal);
			BigDecimal add = preSumTotal.add(newSumTotal);//最终价格
			vo.setSumTotal(add);
			userMapper.updateCart(vo);
		}else {
//			新添加购物车
			userMapper.addCart(vo);
		}
	}
	@Override
	public List<CartVo> findCartListByUid(Integer uid) {
		return userMapper.findCartListByUid(uid);
	}

}
